import { createRouter, createWebHistory } from "vue-router";
import _, { findLastKey } from "lodash";
import { useUserStore } from "@/store/modules/user";
// console.log(modules)
export const childrenRouterMap = [
  {
    path: "/dashboard",
    component: () => import("@/views/dashboard/index.vue"),
    meta: {
      icon: "overview",
      title: "数据总览",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: true,
      isKeepAlive: true,
    },
  },
  {
    path: "/account/assets/freeze",
    component: () => import("@/views/assetDetails/components/freezeAssets.vue"),
    meta: {
      icon: "HomeFilled",
      title: "冻结资产",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/assets",
    },
  },
  {
    path: "/account/assets/pending",
    component: () =>
      import("@/views/assetDetails/components/pendingRecords.vue"),
    meta: {
      icon: "HomeFilled",
      title: "在途资产",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/assets",
    },
  },

  {
    path: "/account/assets/records",
    component: () =>
      import("@/views/assetDetails/components/assetsRecords.vue"),
    meta: {
      icon: "HomeFilled",
      title: "币种明细",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/assets",
    },
  },
  {
    path: "/changePassword",
    component: () => import("@/views/settings/components/ChangePassword.vue"),
    meta: {
      icon: "HomeFilled",
      title: "修改密码",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/dashboard",
    },
  },
  {
    path: "/validator",
    component: () => import("@/views/settings/components/Validator.vue"),
    meta: {
      icon: "HomeFilled",
      title: "匹配验证器应用",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/dashboard",
    },
  },
];

export const adminRoterMap = [
  {
    path: "/merchantSettings",
    meta: {
      icon: "shop",
      title: "商户设置",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
    },
    children: [
      {
        path: "/settings",
        component: () => import("@/views/settings/index.vue"),
        meta: {
          icon: "identity",
          title: "个人设置",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
        },
      },
      {
        path: "/invite",
        component: () => import("@/views/merchantSettings/invite/index.vue"),
        meta: {
          icon: "identity",
          title: "邀请",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
        },
      },
      {
        path: "/merchantSettings/parameters",
        component: () =>
          import("@/views/merchantSettings/parameters/index.vue"),
        meta: {
          icon: "operation",
          title: "商户参数",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
        },
      },
      {
        path: "/merchantSettings/subAccount",
        component: () =>
          import("@/views/merchantSettings/subAccount/index.vue"),
        meta: {
          icon: "subAccount",
          title: "子账号管理",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
        },
      },
      {
        path: "/merchantSettings/subAccount/create",
        component: () =>
          import("@/views/merchantSettings/createSubAccount/index.vue"),
        meta: {
          icon: "Menu",
          title: "创建子账号",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          activeMenu: "/merchantSettings/subAccount",
        },
      },
      {
        path: "/merchantSettings/permissions",
        component: () =>
          import("@/views/merchantSettings/permissions/index.vue"),
        meta: {
          icon: "identity",
          title: "身份权限",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
        },
      },
      {
        path: "/merchantSettings/permissions/create",
        component: () =>
          import(
            "@/views/merchantSettings/permissions/components/createPermissions.vue"
          ),
        meta: {
          icon: "Menu",
          title: "创建身份",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          activeMenu: "/merchantSettings/permissions",
        },
      },
    ],
  },
];
export const asyncRouterMap = [
  {
    path: "/VAAccount",
    meta: {
      icon: "overview",
      title: "VA账户",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: true,
      isKeepAlive: true,
    },
    children: [
      {
        path: "/VAAccount/accountInfo",
        component: () => import("@/views/VAAccount/accountInfo/index.vue"),
        meta: {
          icon: "assets",
          title: "账户信息",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_vaAccount",
        },
      },
      {
        path: "/VAAccount/paymentRecords",
        component: () => import("@/views/VAAccount/paymentRecords/index.vue"),
        meta: {
          icon: "assets",
          title: "收款记录",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_vaAccount",
        },
      },
      {
        path: "/VAAccount/userList",
        component: () => import("@/views/VAAccount/userList/index.vue"),
        meta: {
          icon: "assets",
          title: "客户列表",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_vaAccount",
        },
      },
      {
        path: "/VAAccount/userList/addCustomer",
        component: () =>
          import("@/views/VAAccount/userList/components/addCustomer.vue"),
        name: "addCustomer",
        meta: {
          icon: "assets",
          title: "新增/编辑客户",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          activeMenu: "/VAAccount/userList",
          code: "web_vaAccount",
        },
      },
      {
        path: "/VAAccount/userList/bankCard",
        component: () =>
          import("@/views/VAAccount/userList/components/bankCard.vue"),
        meta: {
          icon: "assets",
          title: "银行卡",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          activeMenu: "/VAAccount/userList",
          code: "web_vaAccount",
        },
      },
    ],
  },

  {
    path: "/assetDetails",
    meta: {
      icon: "assets",
      title: "多币种钱包",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
    },
    children: [
      {
        path: "/account/assets",
        component: () => import("@/views/assetDetails/index.vue"),
        meta: {
          icon: "assets",
          title: "资产明细",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_currency_wallet_assets",
        },
      },
      // {
      //   path: "/account/assets/usdm",
      //   component: () => import("@/views/assetDetails/records.vue"),
      //   meta: {
      //     icon: "orderHistory",
      //     title: "USDM兑换记录",
      //     isLink: "",
      //     isHide: false,
      //     isFull: false,
      //     isAffix: false,
      //     isKeepAlive: true,
      //     code: "web_currency_wallet_usdm",
      //     permCode: "cs",
      //   },
      // },
    ],
  },
  {
    path: "/wallet",
    meta: {
      icon: "acquiring",
      title: "Crypto钱包",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
    },
    children: [
      {
        path: "/wallet/index",
        component: () => import("@/views/wallet/index.vue"),
        meta: {
          icon: "orderHistory",
          title: "币种资产",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_crypto_assets_view",
        },
      },
      {
        path: "/wallet/records",
        component: () => import("@/views/wallet/records.vue"),
        meta: {
          icon: "orderHistory",
          title: "订单记录",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_crypto_order_view",
        },
      },
    ],
  },
  {
    path: "/card",
    meta: {
      icon: "assets",
      title: "商务卡",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
    },
    children: [
      {
        path: "/card/management",
        name: "management",
        component: () => import("@/views/card/cardManagement/index.vue"),
        meta: {
          icon: "assets",
          title: "卡片管理",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          keepAlive: true,
          code: "web_virsual_card",
        },
      },
      {
        path: "/card/management/addCard",
        component: () =>
          import("@/views/card/cardManagement/components/addCard.vue"),
        meta: {
          icon: "assets",
          title: "创建卡片",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          activeMenu: "/card/management",
          code: "web_virsual_card",
        },
      },
      {
        path: "/card/management/editCard",
        name: "nameCard",
        component: () =>
          import("@/views/card/cardManagement/components/editCard.vue"),
        meta: {
          icon: "assets",
          title: "修改卡片",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          activeMenu: "/card/management",
          code: "web_virsual_card",
        },
      },
      {
        path: "/card/management/cardDetail",
        name: "addCard",
        component: () =>
          import("@/views/card/cardManagement/components/cardDetail.vue"),
        meta: {
          icon: "assets",
          title: "卡片详情",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          activeMenu: "/card/management",
          code: "web_virsual_card",
        },
      },
      {
        path: "/card/transactionRecords",
        component: () => import("@/views/card/ordTransactionRecords/index.vue"),
        meta: {
          icon: "assets",
          title: "交易明细",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_virsual_card",
        },
      },
    ],
  },
  {
    path: "/wallet/detail/:coin_name",
    name: "wallet_detail",
    component: () => import("@/views/wallet/detail.vue"),
    meta: {
      icon: "Menu",
      title: "币种详情",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/wallet",
    },
  },
  {
    path: "/swap",
    meta: {
      icon: "exchange",
      title: "币币兑换",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
    },
    children: [
      {
        path: "/swap/placeAnOrder",
        component: () => import("@/views/swap/index.vue"),
        meta: {
          icon: "place",
          title: "下单",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_swap_place_order",
        },
      },
      {
        path: "/swap/order/records",
        component: () => import("@/views/swap/orderRecord.vue"),
        meta: {
          icon: "orderHistory",
          title: "订单记录",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_swap_order",
        },
      },
      {
        path: "/swap/pendingOrder/records",
        component: () => import("@/views/swap/pendingOrderRecord.vue"),
        meta: {
          icon: "pending",
          title: "挂单记录",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true,
          code: "web_swap_publish",
        },
      },
    ],
  },

  {
    path: "/acquiring/order",
    component: () => import("@/views/account/order/index.vue"),
    meta: {
      icon: "acquiring",
      title: "收单",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      code: "web_acquiring",
    },
  },
  {
    path: "/acquiring/order/details",
    component: () => import("@/views/account/order/components/detailsPage.vue"),
    meta: {
      icon: "Menu",
      title: "收单详情",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/acquiring/order",
      code: "web_acquiring_view",
    },
  },

  {
    path: "/account/recharge",
    component: () => import("@/views/account/recharge/index.vue"),
    meta: {
      icon: "topUp",
      title: "充值",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      code: "web_recharge",
    },
  },
  {
    path: "/account/recharge/details",
    component: () =>
      import("@/views/account/recharge/components/detailsPage.vue"),
    meta: {
      icon: "Menu",
      title: "充值详情",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/recharge",
      code: "web_recharge_view",
    },
  },
  {
    path: "/account/recharge/KVBDetails",
    component: () =>
      import("@/views/account/recharge/components/KVBDetails.vue"),
    meta: {
      icon: "Menu",
      title: "充值详情",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/recharge",
      code: "web_recharge_view",
    },
  },
  {
    path: "/account/recharge/create",
    component: () => import("@/views/account/recharge/components/create.vue"),
    meta: {
      icon: "Menu",
      title: "创建充值表单",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/recharge",
      code: "web_recharge_add",
    },
  },
  //  {
  //    path: "/payment",
  //    name: "payment",
  //    meta: {
  //      icon: "payment",
  //      title: "付款",
  //      isLink: "",
  //      isHide: false,
  //      isFull: false,
  //      isAffix: false,
  //      isKeepAlive: true,
  //    },
  //    children: [
  //      {
  //        path: "/account/payment",
  //        component: () => import("@/views/account/payment/index.vue"),
  //        meta: {
  //          icon: "payment",
  //          title: "付款",
  //          isLink: "",
  //          isHide: false,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          code: "web_pay",
  //        },
  //      },
  //
  //      {
  //        path: "/account/customer",
  //        component: () => import("@/views/account/management/index.vue"),
  //        meta: {
  //          icon: "customer",
  //          title: "客户管理",
  //          isLink: "",
  //          isHide: false,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          code: "web_customer",
  //        },
  //      },
  //    ],
  //  },
  //
  //  {
  //    path: "/account/payment/details",
  //    component: () =>
  //      import("@/views/account/payment/components/detailsPage.vue"),
  //    meta: {
  //      icon: "Menu",
  //      title: "付款详情",
  //      isLink: "",
  //      isHide: true,
  //      isFull: false,
  //      isAffix: false,
  //      isKeepAlive: true,
  //      activeMenu: "/account/payment",
  //      code: "web_pay_view",
  //    },
  //  },
  {
    path: "/account/withdraw",
    component: () => import("@/views/account/withdraw/index.vue"),
    meta: {
      icon: "withdraw",
      title: "提现",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      code: "web_withdraw",
    },
  },
  {
    path: "/account/withdraw/details",
    component: () =>
      import("@/views/account/withdraw/components/detailsPage.vue"),
    meta: {
      icon: "Menu",
      title: "提现详情",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/withdraw",
      code: "web_withdraw_view",
    },
  },
  {
    path: "/account/withdraw/card",
    component: () =>
      import("@/views/account/withdraw/components/collectionCard.vue"),
    meta: {
      icon: "Menu",
      title: "银行卡管理",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/withdraw",
      code: "web_withdraw_manage_cards",
    },
  },
  {
    path: "/exchange",
    component: () => import("@/views/exchange/index.vue"),
    meta: {
      icon: "topUp",
      title: "换汇",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      code: "web_exchange_order",
    },
  },

  {
    path: "/account/customer/new",
    component: () =>
      import("@/views/account/management/components/DialogForm.vue"),
    meta: {
      icon: "Menu",
      title: "新增客户",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/customer",
      code: "web_customer_add",
    },
  },
  {
    path: "/account/customer/update",
    component: () =>
      import("@/views/account/management/components/DialogForm.vue"),
    meta: {
      icon: "Menu",
      title: "编辑客户",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/customer",
      code: "web_customer_update",
    },
  },
  {
    path: "/account/customer/details",
    component: () =>
      import("@/views/account/management/components/checkManagementForm.vue"),
    meta: {
      icon: "Menu",
      title: "查看客户详情",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/customer",
      code: "web_customer_view",
    },
  },
  {
    path: "/account/customer/bankCard",
    component: () =>
      import("@/views/account/management/components/bankCard.vue"),
    meta: {
      icon: "Menu",
      title: "银行卡管理",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/account/customer",
      code: "web_customer_manage_cards",
    },
  },

  //  {
  //    path: "/exchange",
  //    meta: {
  //      icon: "exchange",
  //      title: "换汇",
  //      isLink: "",
  //      isHide: false,
  //      isFull: false,
  //      isAffix: false,
  //      isKeepAlive: true,
  //    },
  //    children: [
  //      {
  //        path: "/exchange/market",
  //        component: () => import("@/views/exchange/market/index.vue"),
  //        meta: {
  //          icon: "place",
  //          title: "换汇",
  //          isLink: "",
  //          isHide: false,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          code: "web_exchange_order",
  //        },
  //      },
  //      {
  //        path: "/exchange/market/start",
  //        component: () => import("@/views/exchange/market/components/start.vue"),
  //        meta: {
  //          icon: "place",
  //          title: "换汇",
  //          isLink: "",
  //          isHide: true,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          activeMenu: "/exchange/market",
  //          code: "web_exchange_order_place",
  //        },
  //      },
  //      {
  //        path: "/exchange/order",
  //        component: () => import("@/views/exchange/order/index.vue"),
  //        meta: {
  //          icon: "orderHistory",
  //          title: "订单记录",
  //          isLink: "",
  //          isHide: false,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          code: "web_exchange_order_records",
  //        },
  //      },
  //      {
  //        path: "/exchange/order/detail",
  //        component: () => import("@/views/exchange/order/components/detail.vue"),
  //        meta: {
  //          icon: "Menu",
  //          title: "订单详情",
  //          isLink: "",
  //          isHide: true,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          activeMenu: "/exchange/order",
  //          code: "web_exchange_order_records_view",
  //        },
  //      },
  //      {
  //        path: "/exchange/publish",
  //        component: () => import("@/views/exchange/publish/index.vue"),
  //        meta: {
  //          icon: "pending",
  //          title: "挂单",
  //          isLink: "",
  //          isHide: false,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          code: "web_exchange_ent_records",
  //          permCode: "cep",
  //        },
  //      },
  //      {
  //        path: "/exchange/publish/create",
  //        component: () => import("@/views/exchange/publish/components/create.vue"),
  //        meta: {
  //          icon: "Menu",
  //          title: "创建挂单",
  //          isLink: "",
  //          isHide: true,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          activeMenu: "/exchange/publish",
  //          code: "web_exchange_ent_records_place",
  //        },
  //      },
  //      {
  //        path: "/exchange/publish/edit",
  //        component: () => import("@/views/exchange/publish/components/edit.vue"),
  //        meta: {
  //          icon: "Menu",
  //          title: "编辑挂单",
  //          isLink: "",
  //          isHide: true,
  //          isFull: false,
  //          isAffix: false,
  //          isKeepAlive: true,
  //          activeMenu: "/exchange/publish",
  //          code: "web_exchange_ent_records_edit",
  //        },
  //      },
  //    ],
  //  },
];

export const staticRouter = [
  {
    path: "/",
    redirect: "/dashboard",
  },
  {
    path: "/layout",
    name: "layout",
    component: () => import("@/layout/index.vue"),
    redirect: "/dashboard",
    children: [...childrenRouterMap],
  },
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
    },
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/:pathMatch(.*)",
    name: "not-found",
    meta: {
      title: "404",
    },
    component: () => import("@/views/error/404.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: [...staticRouter],
  strict: false,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

/**
 * 判断用户是否有权限访问单个路由
 * route：访问的路由
 */
const hasPermission = (route) => {
  const useUser = useUserStore();
  const permissionList = useUser.currentPermissions.value;

  if (permissionList[0] === "all") {
    return true;
  }
  if (route.meta && route.meta.code) {
    // 判断route的code值是否存在于permissionList
    return permissionList.includes(route.meta.code);
  }
  return true;
};

/**
 * 筛选可访问的动态路由
 * routerMap：访问的动态列表
 */
const filterAsyncRouter = (routerMap) => {
  const accessedRouters = routerMap.filter((route) => {
    if (hasPermission(route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children);
      }
      return true;
    }
    return false;
  });
  return accessedRouters;
};

export const generateRouter = () => {
  const useUser = useUserStore();
  const permissionList = useUser.currentPermissions.value;
  if (["all"].includes(permissionList[0])) {
    adminRoterMap.forEach((item) => {
      router.addRoute("layout", item);
    });
  }

  const RouterMap = _.cloneDeep(asyncRouterMap);

  return new Promise((resolve) => {
    const validRouter = filterAsyncRouter(RouterMap);
    validRouter.forEach((item) => {
      router.addRoute("layout", item);
    });
    resolve(true);
  });
};

// asyncRouterMap.forEach((item) => {
//     router.addRoute("layout", item);
// })

export default router;
